// pages/shengji/shengji.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    countries: ["VIP会员", "xx", "xx"],
    countryIndex: 0,
    fetch:''
  },
  bindCountryChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);

    this.setData({
      countryIndex: e.detail.value
    })
    var countryIndex = e.detail.value
    var levels = this.data.levels
    for (var i = 0; i < levels.length;i++){
      if (levels[i]['LevelName'] == levels[countryIndex]['LevelName']){

        this.setData({
          amount: levels[i]['upPrice'],
          newlevelid: levels[i]['LevelId'],

        })
      }
    }
    console.log(this.data.amount,'费用')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.vipUp()
    this.setData({
     
      foot: wx.getStorageSync('foot')

    })
  },
  vipUp: function () {//读取会员
    var that = this;
    var userid = wx.getStorageSync('userId');
    wx.request({
      url: app.globalData.apiUrl + 'user/vipUp',
      data: {
        // opt: 'getUser',
        userId: userid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '用户信息')
        if (res.data.levels.length!==0){
          var amount = res.data.levels[0].upPrice ;
            var newlevelid=res.data.levels[0].LevelId;
          }

          that.setData({
            fetch: res.data['fetch'],
            info: res.data,
            levels:res.data.levels,
            amount: amount,
            newlevelid: newlevelid
          })
          
        console.log(that.data.info['fetch'],'等级')
      }

    })
    console.log(that.data.canIUse)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.vipUp();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  //onShareAppMessage: function () {

  //},
  //确认升级
  formSubmit: function (e) {
    console.log(e)

    var that = this;
 
    var formData = e.detail.value;
    wx.request({
      url: app.globalData.apiUrl + 'user/vipUpDo',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500,
            success: function () {
              that.pay(res.data.orderid);
            }
          })


        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  pay: function (id) {//支付
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Pay/shengji_order',
      data: {
        // opt: 'GetUnifiedOrderResult',
        userId: wx.getStorageSync('userId'),
        id: id,//订单id
        // typeId: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '支付返回信息')
        if (res.data != null) {

          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              console.log(res)
              that.onLoad();
            },
            'fail': function (res) {
              // wx.redirectTo({
              //   url: '/pages/order/order',
              // })
            },
            'complete': function (res) {
              // wx.redirectTo({
              //   url: '/pages/order/order',
              // })
            }
          })
        } else {
          console.log("非微信支付方式")
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.redirectTo({
                  url: '/pages/order/order',
                })
              }, 2000) //延迟时间
            },
          })
        }
      }
    })
  },
  qrshengji:function(){
    wx.navigateTo({
      url: 'qrshengji/qrshengji',
    })
  },
  gerenxinxi:function(){
    wx.navigateTo({
      url: '../personal/gerenxinxi/gerenxinxi',
    })
  }
})